/* Copyright 2010 John L. Reilly

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

package com.riq;

import java.io.IOException;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.riq.entity.Department;
import com.riq.entity.Member;

@SuppressWarnings("serial")
public class AdminMemberServlet extends HttpServlet {
  private static final Logger log = Logger.getLogger(Department.class.getName());
  private Long deptId;

  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
    doGet(request, response);
  }
  
  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException { 

  // ---------------------------------------------------------------------------------------------
  //  HEADER: MEMBER
  // ---------------------------------------------------------------------------------------------

    Cookie[] cookie = request.getCookies();
    deptId = Long.valueOf(ServletUtilities.getCookieValue(cookie, "cvDeptId", "99999"));
    String action = request.getParameter("action"); 
    
  // ---------------------------------------------------------------------------------------------
  //  METHOD DIRECTORY: MEMBER
  // ---------------------------------------------------------------------------------------------

    if ("create".equalsIgnoreCase(action)) { 
      create(response, request); 
    } else if ("display".equalsIgnoreCase(action)) { 
      display(response, request);
    } else if ("edit".equalsIgnoreCase(action)) { 
      edit(response, request); 
    } else if ("set".equalsIgnoreCase(action)) { 
      set(response, request); 
    } else if ("delete".equalsIgnoreCase(action)) { 
      delete(response, request); }
  }

  // -----------------------------------------------------------------------------------------------
  //  METHODS
  // -----------------------------------------------------------------------------------------------


  private void create(HttpServletResponse response, ServletRequest request) {

    PersistenceManager pm = PMF.get().getPersistenceManager();

    double dDistance = 0;
    long dDistTimeStamp = 0;
    String lastLat = "";
    String lastLng = "";
    String gpsAccuracy = "";
    String dDistGroup = null;
    String targetObjectType = "";
    Long targetObjectId = null;

    Member m = new Member(
      deptId,
      request.getParameter("firstName"),
      request.getParameter("lastName"),
      request.getParameter("type"),
      request.getParameter("smsAddress"),
      request.getParameter("callerId1"),
      request.getParameter("callerId2"),
      request.getParameter("callerId3"),
      gpsAccuracy,
      dDistance,
      dDistGroup,
      dDistTimeStamp,       
      request.getParameter("cookieHash"),
      request.getParameter("latitudeId"),
      request.getParameter("trackingFlag"),
      lastLat,
      lastLng,
      System.currentTimeMillis(),
      targetObjectId,
      targetObjectType
    );
    pm.makePersistent(m);
    
    try
    {
      response.sendRedirect("department?action=display");
    } catch (IOException e)
    {
      // TODO Auto-generated catch block
        e.printStackTrace();
    }
  }


  private void display(HttpServletResponse response, ServletRequest request) {

    PersistenceManager pm = PMF.get().getPersistenceManager();
    Long memberId = Long.valueOf(request.getParameter("memberId"));
    Member m = pm.getObjectById(Member.class, memberId);
    request.setAttribute("member", m);

    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/memberDisplay.jsp");
    try
    {
      dispatcher.forward(request, response);
    } catch (ServletException e)
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e)
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }  
  }

  
  private void edit(HttpServletResponse response, ServletRequest request) {

    PersistenceManager pm = PMF.get().getPersistenceManager();
    Long memberId =  Long.valueOf(request.getParameter("memberId"));
    Member m = pm.getObjectById(Member.class, memberId);  
    request.setAttribute("member", m);
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/memberEdit.jsp");
    try
    {
      dispatcher.forward(request, response);
    } catch (ServletException e)
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e)
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }


  private void set (HttpServletResponse response, ServletRequest request) {

    PersistenceManager pm = PMF.get().getPersistenceManager();
    Long memberId =  Long.valueOf(request.getParameter("memberId"));
    Member m = pm.getObjectById(Member.class, memberId); 

    m.setdeptId(Long.valueOf(request.getParameter("deptId")));
    m.setfirstName(request.getParameter("firstName"));
    m.setlastName(request.getParameter("lastName"));
    m.settype(request.getParameter("type"));
    m.setsmsAddress(request.getParameter("smsAddress"));
    m.setcallerId1(request.getParameter("callerId1"));
    m.setcallerId1(request.getParameter("callerId2"));
    m.setcallerId1(request.getParameter("callerId3"));
    m.setgpsAccuracy(request.getParameter("gpsAccuracy"));
    m.setdistGroup(request.getParameter("distGroup"));
    m.setcookieHash(request.getParameter("cookieHash"));
    m.setlatitudeId(request.getParameter("latitudeId"));
    m.settrackingFlag(request.getParameter("trackingFlag"));

    pm.makePersistent(m); 
    pm.close();

    try
    {
      response.sendRedirect("department?action=display");
    } catch (IOException e)
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }


  private void delete(HttpServletResponse response, ServletRequest request) {

    PersistenceManager pm = PMF.get().getPersistenceManager();
    Long memberId = Long.valueOf(request.getParameter("memberId"));
    Member m = pm.getObjectById(Member.class, memberId);
    pm.deletePersistent(m);
    pm.close();
    try
    {
      response.sendRedirect("department?action=display");
    } catch (IOException e)
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}

